﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using GPS;

namespace Cartography.Desktop
{
    public partial class ConfGPS : Form
    {
        private ToolStripStatusLabel gpsStatus;
        private FormPrincipal princ;

        public FormPrincipal FormPrincipal
        {
            set
            {
                this.princ = value;
            }
        }

        public ToolStripStatusLabel GPSStatus
        {
            get
            {
                return this.gpsStatus;
            }
            set
            {
                this.gpsStatus = value;
            }
        }

        public ConfGPS()
        {
            InitializeComponent();
        }
        public ComboBox PortComboBox
        {
            get
            {
                return this.portComboBox;
            }
        }
        public ComboBox BPSComboBox
        {
            get
            {
                return this.bpsComboBox;
            }
        }

        private void cancButtonHandler(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void okButtonHandler(object sender, EventArgs e)
        {

            if (FormPrincipal.Instance.Mapa != null)
            {
                string bps = (string)this.bpsComboBox.SelectedItem;
                string port = (string)this.portComboBox.SelectedItem;

                //Reconfigurar el GPS
                if (port==null || bps==null) 
                {
                    MessageBox.Show("Dato(s) invalido(s).");
                    return;
                } 
                GPSService.configure(port, int.Parse(bps));
                GPSService.add(princ);

                this.gpsStatus.Text = "GPS Configurado - Triangulando posición";

                // Lanzar el Thread para la posicion
                Thread t = new Thread(new ThreadStart(GPSService.service));
                t.Start();

                // Si ya había un thread corriendo, matarlo
                if (princ.ThreadGPS != null)
                    princ.ThreadGPS.Abort();

                // Lanzar el nuevo thread
                princ.ThreadGPS = t;

                this.Dispose();
            }
            else 
            {
                MessageBox.Show("Cargar primero las tiles");
            }
        }
    }
}
